public class BinaryOperation {
    // 成员变量
    private int leftOperand;    // 左操作数
    private int rightOperand;   // 右操作数
    private char operator;      // 运算符
    private int value;          // 计算结果

    // 构造函数
    public BinaryOperation(int leftOperand, int rightOperand, char operator) {
        this.leftOperand = leftOperand;
        this.rightOperand = rightOperand;
        this.operator = operator;
        this.value = calculate();
    }

    // 计算方法，确保结果在0-100范围内
    private int calculate() {
        int result = 0;
        switch (operator) {
            case '+':
                result = leftOperand + rightOperand;
                // 确保加法结果不超过100
                result = Math.min(100, result);
                break;
            case '-':
                result = leftOperand - rightOperand;
                // 确保减法结果非负
                result = Math.max(0, result);
                break;
            default:
                throw new IllegalArgumentException("不支持的运算符: " + operator);
        }
        return result;
    }

    // Getter方法
    public int getLeftOperand() {
        return leftOperand;
    }

    public int getRightOperand() {
        return rightOperand;
    }

    public char getOperator() {
        return operator;
    }

    public int getValue() {
        return value;
    }

    // toString方法，返回格式化的算式字符串
    @Override
    public String toString() {
        return leftOperand + "" + operator + rightOperand + "=" + value;
    }

    // equals方法，比较两个算式是否相等
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        BinaryOperation that = (BinaryOperation) obj;
        return leftOperand == that.leftOperand &&
               rightOperand == that.rightOperand &&
               operator == that.operator;
    }

    // 生成随机加法算式的静态方法（确保结果在0-100范围内）
    public static BinaryOperation generateAdditionOperation() {
        int left = (int) (Math.random() * 101);  // 0-100的随机数
        int right = (int) (Math.random() * (101 - left)); // 确保和不超过100
        return new BinaryOperation(left, right, '+');
    }

    // 生成随机减法算式的静态方法（确保结果在0-100范围内）
    public static BinaryOperation generateSubtractOperation() {
        int left = (int) (Math.random() * 101);  // 0-100的随机数
        int right = (int) (Math.random() * (left + 1)); // 确保right <= left，结果非负且不超过100
        return new BinaryOperation(left, right, '-');
    }
}